<?php


namespace app\admin\controller;


use app\admin\model\ShopLevel;
use app\admin\service\AdminService;
use app\admin\service\AuthRuleService;
use app\admin\service\StatisticsService;
use app\common\controller\BaseAdmin;
use app\admin\model\Admin as AdminModel;
use app\admin\model\User as userModel;
use app\admin\model\ShopLevel as ShopLevelModel;
use app\admin\model\Shop as ShopModel;
use app\admin\model\Statistics as StatisticsModel;
use think\facade\Config;
use crypt\Crypt;

class Index extends BaseAdmin
{

    protected $check_login = false;

    public function index(){
        $this->checkLogin();

        //获取登录用户信息
        $this->assign('admin',AdminService::getLoginInfo());

        return $this->fetch();
    }


    /**
     * 权限菜单
     * @return \think\response\Json
     */
    public function menu(){
        $this->checkLogin();
        return json(AuthRuleService::authAllMenu($this->auth_uid));
    }

    /**
     * 监测后台登录账号是否存在
     */
    public function check(){
        if ($this->request->isAjax()){
            $username = $this->request->post('username');
            $admin = AdminModel::where('username|email',$username)->find();
            if (!$admin){
                $this->error('账号不存在');
            }
            if ($admin['state']==0){
                $this->error('账号已冻结请联系管理员');
            }
            $this->success('用户正常');
        }
    }

    /**
     * 后台管理员登录
     * @return array|string
     * @throws \Exception
     */
    public function login(){
        if ($this->request->isAjax()){
            $data = $this->request->post();
            return AdminService::login($data);
        }
        return $this->fetch();
    }

    /**
     * 后台登录记住密码
     * @return \think\response\Json
     */
    public function remember(){
        if(!cookie('remember')){
            return json(['code'=>1,'data'=>[]]);
        }
        $remember = Crypt::decrypt(cookie('remember'),Config::get('login_admin.crypt'));
        return json(['code'=>1,'data'=>unserialize($remember)]);
    }

    /**
     * 退出登录
     */
    public function logout(){
        AdminService::logout();
       return json(['code'=>0,'msg'=>'成功退出','data'=>[]]);
    }



    /**
     * 控制台
     * @return mixed
     */
    public function console(){
        if ($this->request->isAjax()){
            $lists = StatisticsService::getGroupData();
            return $this->tableJson($lists,count($lists));
        }
        return $this->fetch();
    }

    /**
     * 店铺统计
     * @return mixed
     */
    public function shop(){
        if ($this->request->isAjax()){
              $levelList = ShopLevel::select();
              $lists = [];
              foreach ($levelList as $key => $level){
                  $lists[$key]['level'] = $level->level;
                  $lists[$key]['title'] = $level->name;
                  $lists[$key]['shop_num'] = ShopModel::where('shop_level_id',$level->id)
                      ->whereDay('create_time')
                      ->count();
              }
              return $this->tableJson($lists,count($lists));
        }
    }

    /**
     * 充值提款统计
     * @return array|void
     */
    public function amount(){
        if ($this->request->isAjax()){
            $lists_data  = [
                'day_recharge_amount'=>'当日总充值金额',
                'day_withdraw_amount'=>'当日总提现金额',
                'day_recharge_order' => '当日总充值笔数',
                'day_withdraw_order' => '当日总提现笔数',
                'activity_order' => '当日福利订单数',
                'activity_group_order' => '当日拼单成功数',
            ];
            $lists = [];
            foreach ($lists_data as $key => $value){
                $lists[$key]['title'] = $value;
                //所有代理,之和
                $lists[$key]['amount'] = StatisticsModel::whereDay('date_time')->sum($key);
            }
            return $this->tableJson($lists,count($lists));
        }
    }

    /**
     * 人数统计
     */
    public function num(){
        if ($this->request->isAjax()){
            $lists_data  = [
                'day_register_num'=>'当日总注册人数',
                'day_recharge_num'=>'当日充值人数',
                'recharge_balance_amount' => '充值成员总余额',
                'admin_recharge_amount' => '当日后台加款',
                'agent_recharge_amount' => '当日代理加款',
                'order_pay_amount' =>'当日订单支付',
                'order_res_amount' => '当日订单回款',
                'order_com_amount' => '当日佣金支出',
            ];
            $lists = [];
            foreach ($lists_data as $key => $value){
                $lists[$key]['title'] = $value;
                //所有代理,之和
                $lists[$key]['amount'] = StatisticsModel::whereDay('date_time')->sum($key);
            }
            return $this->tableJson($lists,count($lists));
        }
    }

}
